package com.spic.business.plan.exerciseDetail.service.impl;

import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.plan.book.domain.PlanStandingBook;
import com.spic.business.plan.book.service.IPlanStandingBookService;
import com.spic.business.plan.exerciseDetail.domain.PlanExerciseDetail;
import com.spic.business.plan.exerciseDetail.mapper.PlanExerciseDetailMapper;
import com.spic.business.plan.exerciseDetail.service.IPlanExerciseDetailService;
import com.spic.common.core.domain.R;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 应急预案演练计划明细Service业务层处理
 *
 * @author spic
 * @date 2021-11-09
 */
@Service
public class PlanExerciseDetailServiceImpl implements IPlanExerciseDetailService
{
    @Autowired
    private PlanExerciseDetailMapper planExerciseDetailMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private IPlanStandingBookService planStandingBookService;
    /**
     * 查询应急预案演练计划明细
     *
     * @param id 应急预案演练计划明细主键
     * @return 应急预案演练计划明细
     */
    @Override
    public PlanExerciseDetail selectPlanExerciseDetailById(String id)
    {
        return planExerciseDetailMapper.selectPlanExerciseDetailById(id);
    }

    /**
     * 查询应急预案演练计划明细列表
     *
     * @param planExerciseDetail 应急预案演练计划明细
     * @return 应急预案演练计划明细
     */
    @Override
    @BusinessDataScope(menuPath = "business/plan/exerciseDetail/index")
    public List<PlanExerciseDetail> selectPlanExerciseDetailList(PlanExerciseDetail planExerciseDetail)
    {
        List<PlanExerciseDetail> PlanExerciseDetailList = planExerciseDetailMapper.selectPlanExerciseDetailList(planExerciseDetail);
        PlanExerciseDetailList.forEach(deptList -> {
            //部门ID转码
//            String workUnit = deptList.getDeptId();
//            Long deadline = Long.parseLong(workUnit);
//            R<SysDept> sysDept1 = remoteUserService.lists(deadline);
//            if (sysDept1.getData() != null) {
//                deptList.setDeptId(sysDept1.getData().getDeptName());
//            }
            //用户ID转码
            String userId = deptList.getUserId();
            Long user = Long.parseLong(userId);
            R<SysUser> sysAcceptor = remoteUserService.getUserById(user);
            if (sysAcceptor.getData() != null) {
                deptList.setUserId(sysAcceptor.getData().getUserName());
            }
            //预案ID转码
            String bookId = deptList.getStandingBookId();
            Long book = Long.parseLong(bookId);
            PlanStandingBook planStandingBook = planStandingBookService.selectPlanStandingBookById(book);
            if (planStandingBook != null) {
                deptList.setStandingBookId(planStandingBook.getName());
            }
        });
        return PlanExerciseDetailList;
    }

    /**
     * 新增应急预案演练计划明细
     *
     * @param planExerciseDetail 应急预案演练计划明细
     * @return 结果
     */
    @Override
    public int insertPlanExerciseDetail(PlanExerciseDetail planExerciseDetail)
    {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        planExerciseDetail.setId(uuid);
        return planExerciseDetailMapper.insertPlanExerciseDetail(planExerciseDetail);
    }

    /**
     * 修改应急预案演练计划明细
     *
     * @param planExerciseDetail 应急预案演练计划明细
     * @return 结果
     */
    @Override
    public int updatePlanExerciseDetail(PlanExerciseDetail planExerciseDetail)
    {
        planExerciseDetail.setUpdateTime(DateUtils.getNowDate());
        planExerciseDetail.setUpdateBy(SecurityUtils.getUsername());
        return planExerciseDetailMapper.updatePlanExerciseDetail(planExerciseDetail);
    }

    /**
     * 批量删除应急预案演练计划明细
     *
     * @param ids 需要删除的应急预案演练计划明细主键
     * @return 结果
     */
    @Override
    public int deletePlanExerciseDetailByIds(String[] ids)
    {
        return planExerciseDetailMapper.deletePlanExerciseDetailByIds(ids);
    }

    /**
     * 删除应急预案演练计划明细信息
     *
     * @param id 应急预案演练计划明细主键
     * @return 结果
     */
    @Override
    public int deletePlanExerciseDetailById(String id)
    {
        return planExerciseDetailMapper.deletePlanExerciseDetailById(id);
    }
}
